package com.card.client;

import com.card.client.dialog.ChangeUserInfoDialog;
import com.card.client.dialog.LoginDialog;
import com.card.client.entry.UserInfo;
import com.card.client.entry.Util;
import com.card.client.service.UserInfoManageService;
import com.card.client.service.UserInfoManageServiceAsync;
import com.card.client.view.CreditCardBillInfoView;
import com.card.client.view.CreditCardView;
import com.card.client.view.FinanceTypeConfigView;
import com.card.client.view.HomeView;
import com.card.client.view.InputAccountsView;
import com.card.client.view.OutputAccountsView;
import com.card.client.view.UserInfoManageView;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
import com.google.gwt.user.client.Window.ClosingHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Card implements EntryPoint {
	
	LoginDialog ld = null;
	
	UserInfo loginedUser = null;
	
	private HomeView homeView;
	private CreditCardView creditCardView;
	private CreditCardBillInfoView creditCardBillInfoView;
	private FinanceTypeConfigView financeTypeConfigView;
	private InputAccountsView inputAccountsView;
	private OutputAccountsView outputAccountsView;
	private UserInfoManageView userInfoManageView;
	
	private final UserInfoManageServiceAsync uims = GWT.create(UserInfoManageService.class);
	
	public void onModuleLoad() {
		initAllView();
		Window.addWindowClosingHandler(new ClosingHandler() {
			public void onWindowClosing(ClosingEvent event) {
				Window.alert("欢迎再次使用!");
			}
		});
		uims.getLoginedUser(new AsyncCallback<UserInfo>() {
			public void onSuccess(UserInfo result) {
				if(result==null){
					ld = new LoginDialog();
					ld.show();
					ld.addCloseHandler(new CloseHandler<PopupPanel>() {
						public void onClose(CloseEvent<PopupPanel> event) {
							if(ld.isClose()){
								return;
							} else {
								if(ld.isLogin()){
									initWidget(ld.getLoginedUser());
								}
							}
						}
					});
				} else {
//		UserInfo test = new UserInfo();
//		test.setCompetence(3);
//		test.setLastLoginTime(new Date());
//		test.setName("test");
//		test.setLoginName("test");
//		test.setPass("test");
					initWidget(result);
				}
			}
			public void onFailure(Throwable caught) {}
		});
	}

	private void initAllView() {
		homeView = new HomeView();
		creditCardView = new CreditCardView();
		creditCardBillInfoView = new CreditCardBillInfoView();
		financeTypeConfigView = new FinanceTypeConfigView();
		inputAccountsView = new InputAccountsView();
		outputAccountsView = new OutputAccountsView();
		userInfoManageView = new UserInfoManageView();
	}

	private void initWidget(UserInfo loginedUser) {
		RootPanel.get("main").add(homeView);
		this.loginedUser = loginedUser;
		RootPanel.get("menu").add(initMenu());
	}
	
	private Widget initMenu() {
		MenuBar menu = new MenuBar();
	    menu.setAutoOpen(false);
	    menu.setWidth("100%");
	    menu.setHeight("25");
	    menu.setAnimationEnabled(false);

	    MenuBar financialMenu = new MenuBar(true);
	    financialMenu.setAnimationEnabled(false);
	    MenuComm InputAccountsComm = new MenuComm(Util.INPUT_ACCOUNTS);
	    financialMenu.addItem("收入记录", InputAccountsComm);
	    MenuComm OutputAccountsComm = new MenuComm(Util.OUTPUT_ACCOUNTS);
	    financialMenu.addItem("支出记录", OutputAccountsComm);
	    MenuComm CreditCardAccountsComm = new MenuComm(Util.CREDIT_CARD_BILL);
	    financialMenu.addItem("信用卡", CreditCardAccountsComm);
	    if(loginedUser.getCompetence()<Util.USER_COMPETENCE_ULTIMATE){
	    	MenuItem financialItem = new MenuItem("财务统计", financialMenu);
	    	menu.addItem(financialItem);
	    }
	    
	    MenuBar financialAboutMenuBar = new MenuBar(true);
	    financialAboutMenuBar.setAnimationEnabled(false);
	    
	    MenuBar dialyFinanceSumSubMenuBar = new MenuBar(true);
	    dialyFinanceSumSubMenuBar.setAnimationEnabled(false);
	    MenuComm inputOutputAccountSum = new MenuComm("");
	    dialyFinanceSumSubMenuBar.addItem("日常收支统计", inputOutputAccountSum);
	    MenuComm creditCardSum = new MenuComm("");
	    dialyFinanceSumSubMenuBar.addItem("信用卡统计", creditCardSum);
	    
	    MenuItem dialyFinanceSumSubMenuItem = new MenuItem("财务统计", dialyFinanceSumSubMenuBar);
	    financialAboutMenuBar.addItem(dialyFinanceSumSubMenuItem);
	    
	    financialAboutMenuBar.addSeparator();
	    
	    MenuBar financialAnalysisSubMenuBar = new MenuBar(true);
	    financialAnalysisSubMenuBar.setAnimationEnabled(false);
	    MenuComm DailyAnalysisComm = new MenuComm("");
	    financialAnalysisSubMenuBar.addItem("日常账目分析", DailyAnalysisComm);
	    MenuComm CreditCardAnalysisComm = new MenuComm("");
	    financialAnalysisSubMenuBar.addItem("信用卡分析", CreditCardAnalysisComm);
	    MenuItem financialAnalysisSubMenuItem = new MenuItem("财务分析", financialAnalysisSubMenuBar);
	    financialAboutMenuBar.addItem(financialAnalysisSubMenuItem);
	    
	    MenuItem financialAboutMenuItem = new MenuItem("统计与分析", financialAboutMenuBar);
	    if(loginedUser.getCompetence() >= Util.USER_COMPETENCE_MIDDLE){
	    	menu.addItem(financialAboutMenuItem);
	    }
	    
	    menu.addSeparator();
	    
	    MenuBar sysConfigMenu = new MenuBar(true);
	    sysConfigMenu.setAnimationEnabled(false);
		MenuComm userInfoManageComm = new MenuComm(Util.USER_INFO_MANAGE);
		
		if(loginedUser.getCompetence() >= Util.USER_COMPETENCE_HEIGHT){
		    sysConfigMenu.addItem("用户管理", userInfoManageComm);
		    sysConfigMenu.addSeparator();
	    }
		
//	    MenuBar subMenuBar = new MenuBar(true);
//	    subMenuBar.setAnimationEnabled(true);
//	    MenuComm cardFinanceMonthComm = new MenuComm(Util.CARD_FINANCE_MONTH);
//	    subMenuBar.addItem("信用卡财务月管理", cardFinanceMonthComm);
//	    MenuComm dailyFinanceMonthComm = new MenuComm(Util.DIALY_FINANCE_MONTH);
//	    subMenuBar.addItem("日常财务月管理", dailyFinanceMonthComm);
//	    MenuItem subMenuItem = new MenuItem("财务月管理", subMenuBar);
//	    sysConfigMenu.addItem(subMenuItem);
//	    sysConfigMenu.addSeparator();
	    MenuComm cardConfigComm = new MenuComm(Util.CREDIT_CARD);
	    sysConfigMenu.addItem("信用卡管理", cardConfigComm);
	    MenuComm financeTypeConfigComm = new MenuComm(Util.FINANCE_TYPE_CONFIG);
	    sysConfigMenu.addItem("财务类型管理", financeTypeConfigComm);
	    MenuItem sysConfigItem = new MenuItem("功能设置", sysConfigMenu);
	    
	    if(loginedUser.getCompetence() >= Util.USER_COMPETENCE_MIDDLE){
	    	menu.addItem(sysConfigItem);
	    }
	    
	    if(loginedUser.getCompetence()<Util.USER_COMPETENCE_ULTIMATE){
	    	MenuComm changePass = new MenuComm(Util.CHANGE_PASS);
	    	menu.addItem("修改密码", changePass);
	    }
	    
	    MenuComm logout = new MenuComm(Util.LOGOUT);
	    menu.addItem("退出系统", logout);
	    
	    menu.ensureDebugId("menuBar");
		return menu;
	}
	
	class MenuComm implements Command {

		private String flag;
		
		public MenuComm(String flag){
			this.flag = flag;
		}
		
		public void execute() {
			int count = RootPanel.get("main").getWidgetCount();
			if(count>0){
				RootPanel.get("main").remove(0);
			}
			if(flag.equals(Util.CREDIT_CARD)) {
				RootPanel.get("main").add(creditCardView);
				creditCardView.onLoad();
				return;
			}
			if(flag.equals(Util.CREDIT_CARD_BILL)) {
				creditCardBillInfoView = new CreditCardBillInfoView();
				RootPanel.get("main").add(creditCardBillInfoView);
				creditCardBillInfoView.onLoad(Util.getThisMonth());
				return;
			}
			if(flag.equals(Util.FINANCE_TYPE_CONFIG)) {
				RootPanel.get("main").add(financeTypeConfigView);
				financeTypeConfigView.onLoad();
				return;
			}
			if(flag.equals(Util.INPUT_ACCOUNTS)) {
				inputAccountsView = new InputAccountsView();
				RootPanel.get("main").add(inputAccountsView);
				inputAccountsView.onLoad();
				return;
			}
			if(flag.equals(Util.OUTPUT_ACCOUNTS)) {
				outputAccountsView = new OutputAccountsView();
				RootPanel.get("main").add(outputAccountsView);
				outputAccountsView.onLoad();
				return;
			}
			if(flag.equals(Util.USER_INFO_MANAGE)) {
				RootPanel.get("main").add(userInfoManageView);
				userInfoManageView.onLoad();
				return;
			}
			if(flag.equals(Util.CHANGE_PASS)){
				uims.getLoginedUser(new AsyncCallback<UserInfo>() {
					public void onSuccess(UserInfo result) {
						loginedUser = result;
						ChangeUserInfoDialog cuid = new ChangeUserInfoDialog("修改资料", "修改用户资料", result);
						cuid.show();
					}
					public void onFailure(Throwable caught) {}
				});
			}
			if(flag.equals(Util.LOGOUT)){
				RootPanel.get("menu").remove(0);
				uims.removeLoginedUser(new AsyncCallback<Void>() {
					public void onSuccess(Void result) {}
					public void onFailure(Throwable caught) {}
				});
				loginedUser = null;
				ld = new LoginDialog();
				ld.show();
				ld.addCloseHandler(new CloseHandler<PopupPanel>() {
					public void onClose(CloseEvent<PopupPanel> event) {
						if(ld.isClose()){
							return;
						} else {
							if(ld.isLogin()){
								initWidget(ld.getLoginedUser());
							}
						}
					}
				});
			}
		}
	}
}
